package cn.dvptech.remotecontrol.screenmirror.virtualDisplay;

import android.opengl.GLES20;
import android.opengl.GLES30;

import timber.log.Timber;

public class ShaderUtils {
    private static final String TAG = "ShaderUtils";

    public static int compileVertexShader20(String str) {
        return compileShader20(GLES30.GL_VERTEX_SHADER, str);
    }

    public static int compileFragmentShader20(String str) {
        return compileShader20(GLES30.GL_FRAGMENT_SHADER, str);
    }


    // 编译顶点着色器，使用指定的着色器类型和源代码
    public static int compileVertexShader30(String source) {
        return compileShader30(GLES30.GL_VERTEX_SHADER, source);
    }

    // 编译片段着色器，使用指定的着色器类型和源代码
    public static int compileFragmentShader30( String source) {
        return compileShader30(GLES30.GL_FRAGMENT_SHADER, source);
    }

    // 编译着色器，使用指定的着色器类型和源代码
    private static int compileShader30(int shaderType, String source) {
        int shader = GLES30.glCreateShader(shaderType);
        if (shader != 0) {
            GLES30.glShaderSource(shader, source);
            GLES30.glCompileShader(shader);
            int[] compileStatus = new int[1];
            GLES30.glGetShaderiv(shader, GLES30.GL_COMPILE_STATUS, compileStatus, 0);
            if (compileStatus[0] == 0) {
                String shaderTypeString = (shaderType == GLES30.GL_VERTEX_SHADER) ? "Vertex" : "Fragment";
                Timber.e("%s shader compilation error: %s", shaderTypeString, GLES30.glGetShaderInfoLog(shader));
                GLES30.glDeleteShader(shader);
                return 0;
            }
            return shader;
        }
        return 0;
    }

    // 编译着色器，使用指定的着色器类型和源代码
    private static int compileShader20(int shaderType, String source) {
        int shader = GLES20.glCreateShader(shaderType);
        if (shader != 0) {
            GLES20.glShaderSource(shader, source);
            GLES20.glCompileShader(shader);
            int[] compileStatus = new int[1];
            GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
            if (compileStatus[0] == 0) {
                String shaderTypeString = (shaderType == GLES30.GL_VERTEX_SHADER) ? "Vertex" : "Fragment";
                Timber.e("%s shader compilation error: %s", shaderTypeString, GLES20.glGetShaderInfoLog(shader));
                GLES20.glDeleteShader(shader);
                return 0;
            }
            return shader;
        }
        return 0;
    }

    // 链接程序，使用指定的顶点和片段着色器
    public static int linkProgram30(int vertexShader, int fragmentShader) {
        int program = GLES30.glCreateProgram();
        if (program != 0) {
            GLES30.glAttachShader(program, vertexShader);
            GLES30.glAttachShader(program, fragmentShader);
            GLES30.glLinkProgram(program);
            int[] linkStatus = new int[1];
            GLES30.glGetProgramiv(program, GLES30.GL_LINK_STATUS, linkStatus, 0);
            if (linkStatus[0] == 0) {
                Timber.e("程序链接错误: %s", GLES30.glGetProgramInfoLog(program));
                GLES30.glDeleteProgram(program);
                return 0;
            }
            return program;
        }
        return 0;
    }

    // 链接程序，使用指定的顶点和片段着色器
    public static int linkProgram20(int vertexShader, int fragmentShader) {
        int program = GLES20.glCreateProgram();
        if (program != 0) {
            GLES20.glAttachShader(program, vertexShader);
            GLES20.glAttachShader(program, fragmentShader);
            GLES20.glLinkProgram(program);
            int[] linkStatus = new int[1];
            GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
            if (linkStatus[0] == 0) {
                Timber.e("程序链接错误: %s", GLES20.glGetProgramInfoLog(program));
                GLES20.glDeleteProgram(program);
                return 0;
            }
            return program;
        }
        return 0;
    }

    // 验证程序以检查其状态
    public static boolean validProgram30(int program) {
        GLES30.glValidateProgram(program);
        int[] validateStatus = new int[1];
        GLES30.glGetProgramiv(program, GLES30.GL_VALIDATE_STATUS, validateStatus, 0);
        return validateStatus[0] != 0;
    }
}
